!-------------------------------------------------------------LICENSE--------------------------------------------------------------!
!                                                                                                                                  !
!The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    !
!and Message Passing Interface (MPI) parallelization.                                                                              !
!                                                                                                                                  !
!Copyright (C) 2012                                                                                                                !
!Ronglin Jiang                                                                                                                     !
!rljiang@ssc.net.cn                                                                                                                !
!585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             !
!                                                                                                                                  !
!This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         !
!as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             !
!                                                                                                                                  !
!This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    !
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        !
!                                                                                                                                  !
!You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     !
!Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   !
!                                                                                                                                  !
!-------------------------------------------------------------LICENSE--------------------------------------------------------------!

!==================================================================================================================================|
subroutine data_collect(var, ro, gp, mp, te, beta, vx, vy, vz, bx, by, bz, jx, jy, jz,                                             &
               fgx, fgy, fgz, fggx, fggy, fggz, fmgx, fmgy, fmgz, fmtx, fmty, fmtz, var_flag,                                      &
               x_arr, y_arr, z_arr, x, y, z,                                                                                       &
               n_var, nx_block, ny_block, nz_block, n_blocks_all, n_var_total, i_block)
!==================================================================================================================================|

   implicit none

   include 'data_manage_precision.include'

   integer(4), intent(in) :: n_var, nx_block, ny_block, nz_block, n_blocks_all, n_var_total, i_block

   logical(4), intent(in) :: var_flag(n_var_total)

   real(8), intent(inout) :: x(nx_block), x_arr(nx_block, n_blocks_all)
   real(8), intent(inout) :: y(ny_block), y_arr(ny_block, n_blocks_all)
   real(8), intent(inout) :: z(nz_block), z_arr(nz_block, n_blocks_all)
   real(output_real8_flag), intent(inout) :: var(n_var, nx_block, ny_block, nz_block, n_blocks_all)
   real(output_real8_flag), dimension(nx_block, ny_block, nz_block), intent(in) ::                                                 &
                                          ro, gp, mp, te, beta, vx, vy, vz, bx, by, bz, jx, jy, jz,                                &
                                          fgx, fgy, fgz, fggx, fggy, fggz, fmgx, fmgy, fmgz, fmtx, fmty, fmtz

   integer(4) :: i, j, k, i_var, j_var

!----------------------------------------------------------------------------------------------------------------------------------|
   x_arr(:, i_block) = x
   y_arr(:, i_block) = y
   z_arr(:, i_block) = z

   do k = 1, nz_block
   do j = 1, ny_block
   do i = 1, nx_block
      i_var = 1
      j_var = 1

      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = ro(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = gp(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = mp(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = te(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = beta(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = vx(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = vy(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = vz(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = vx(i, j, k)
         i_var = i_var + 1
         var(i_var, i, j, k, i_block) = vy(i, j, k)
         i_var = i_var + 1
         var(i_var, i, j, k, i_block) = vz(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = bx(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = by(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = bz(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = bx(i, j, k)
         i_var = i_var + 1
         var(i_var, i, j, k, i_block) = by(i, j, k)
         i_var = i_var + 1
         var(i_var, i, j, k, i_block) = bz(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = jx(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = jy(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = jz(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = jx(i, j, k)
         i_var = i_var + 1
         var(i_var, i, j, k, i_block) = jy(i, j, k)
         i_var = i_var + 1
         var(i_var, i, j, k, i_block) = jz(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = fgx(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = fgy(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = fgz(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = fgx(i, j, k)
         i_var = i_var + 1
         var(i_var, i, j, k, i_block) = fgy(i, j, k)
         i_var = i_var + 1
         var(i_var, i, j, k, i_block) = fgz(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = fggx(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = fggy(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = fggz(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = fggx(i, j, k)
         i_var = i_var + 1
         var(i_var, i, j, k, i_block) = fggy(i, j, k)
         i_var = i_var + 1
         var(i_var, i, j, k, i_block) = fggz(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = fmgx(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = fmgy(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = fmgz(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = fmgx(i, j, k)
         i_var = i_var + 1
         var(i_var, i, j, k, i_block) = fmgy(i, j, k)
         i_var = i_var + 1
         var(i_var, i, j, k, i_block) = fmgz(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = fmtx(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = fmty(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = fmtz(i, j, k)
         i_var = i_var + 1
      endif
      j_var = j_var + 1
      if (var_flag(j_var)) then
         var(i_var, i, j, k, i_block) = fmtx(i, j, k)
         i_var = i_var + 1
         var(i_var, i, j, k, i_block) = fmty(i, j, k)
         i_var = i_var + 1
         var(i_var, i, j, k, i_block) = fmtz(i, j, k)
         i_var = i_var + 1
      endif
   enddo
   enddo
   enddo

!----------------------------------------------------------------------------------------------------------------------------------|
   return
end subroutine data_collect
